package cn.hxh.page.controller;

import cn.hxh.page.pojo.Config;
import cn.hxh.page.pojo.Link;
import cn.hxh.page.pojo.User;
import cn.hxh.page.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2020/10/6 12:55
 */
@Controller
public class LinkController {

    @Autowired
    private LinkService linkService;

    @ResponseBody
    @RequestMapping("/link/addLink")
    public Integer addLink(@RequestParam("url") String url,
                           @RequestParam("title") String title,
                           HttpServletRequest request) {
        Integer linkFlag = 0;
        try {
            User user = (User) request.getSession().getAttribute(Config.USER_INFO);
            linkService.addLink(title, url, user.getId());
            linkFlag = 1;
        } catch (Exception e) {
            linkFlag = 0;
        }

        return linkFlag;
    }


    @ResponseBody
    @RequestMapping("/link/selectAllLink")
    public List<Link> selectAllLink(HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<Link> linkList = null;
        User userSession = (User) session.getAttribute(Config.USER_INFO);
        if (userSession != null) {
            linkList = linkService.selectAllLink(userSession.getId());
        }
        return linkList;
    }

    @ResponseBody
    @RequestMapping("/link/deleteId")
    public void deleteId(@RequestParam("id")int id) {
        linkService.deleteId(id);
    }


}
